var $ = getApp(), methods = require("../../methods.js");
Page(Object.assign({}, methods, {
	data: {
        leixing:1,
        datalist: [],
        pagenum: 1,
        showModal: false,
        agreement:'',

	},
	_onLoad:function(){
		var that = this
		if(that.options && that.options.cid){
			that.setData({cid:that.options.cid});
        }
        var leixing = this.data.leixing;
        $.get('yuxun/getmydiaodian', { leixing:leixing }, function (res) {
            that.setData(res);
        });
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
    that.getDataList();
    that.getAgreet();
  },
  // 协议
  getAgreet(){
    let that = this
    $.get('yuxun/fatiediaodian4',{},function(res){
        console.log('协议',res)
        that.setData({
          agreement:res.xieyicontent
        })
    })
  },
  // 弹窗
  submit: function() {
    this.setData({
    showModal: true
    })
},

preventTouchMove: function() {

},


go: function() { 
    this.setData({
    showModal: false
    })
},

	getDataList: function () {
		var that = this
		var pagenum = that.data.pagenum
		that.setData({loaddingmore:true})
        $.post('yuxun/getmydiaodian', { pagenum: pagenum, leixing: that.data.leixing}, function (res) {
			that.setData({loaddingmore:false})
			var data = res.data
			if (pagenum == 1) {
        that.setData({ datalist: data});
        
				if (data.length == 0) {
					$.showEmpty('暂无相关数据！');
				}
			} else {
				if (data.length == 0) {
					that.setData({ $nomoreData: true });
				} else {
					var datalist = that.data.datalist
					var newdata = datalist.concat(data)
					that.setData({ datalist: newdata });
				}
			}
		},false);
	},
	onReachBottom: function () {
		if (!this.data.$dataEmpty.visible && !this.data.$nomoreData) {
			this.setData({ pagenum: this.data.pagenum + 1 })
			this.getDataList();
		}
    },
    changetab:function(e){
        var that = this;
        var leixing = e.currentTarget.dataset.leixing
        that.setData({ leixing: leixing })
            that.setData({ pagenum: 1, datalist: [], $nomoreData: false });
            that.getDataList();
    },
    del:function(e){
        var id = e.currentTarget.dataset.id;
        $.post('luntan/deltie', { id: id}, function (res) {
            if(res.status==1){
                var datalist = that.data.datalist;
                datalist.splice(id,1);
                console.log(datalist)
		        that.setData({datalist:datalist});
            }else{
                $.error(res.msg);
            }
        })
    },
    zan:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
         // 获取当前项的索引
        const index = this.data.datalist.findIndex(item => item.id === id)
        $.post('luntan/zan', { id: id}, function (res) {
            if(res.status==1){
                // 取反当前项的点赞状态
                if(res.type==1){
                    that.setData({
                        [`datalist[${index}].zan`]: that.data.datalist[index].zan+1,
                        [`datalist[${index}].islike`]: 1
                     })
                }else{
                    that.setData({
                        [`datalist[${index}].zan`]: that.data.datalist[index].zan-1,
                        [`datalist[${index}].islike`]: 0
                     })
                }
                // 判断当前项是否已点赞
                const className = res.type ? 'like' : ''
                const selector = `.list-item:nth-child(${index + 1}) .zan`
                const btn = that.selectComponent(selector)
                btn && btn.setData({ className })
                
            }else{
                $.error(res.msg);return false;
            }
        })
    },
    // 开启抽号
    open(e){
      console.log('开启抽号')
      console.log('取消钓位',e,e.currentTarget.dataset.id,e.currentTarget.dataset.ischouhao)
      const id = e.currentTarget.dataset.id
      const chouhao = e.currentTarget.dataset.ischouhao
      if (chouhao == 1) {
        wx.showModal({
          title: '提示',
          content: '已抽号，不可再次抽号',
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
              
            }
          }
        })
        return
      }
      wx.showModal({
        title: '提示',
        content: '确定开启抽号吗',
        // cancelText:'删除钓位',
        confirmText:'确定开启',
        complete: (res) => {
          if (res.cancel) {
            
            // $.goto('/diaoyu/pages/openDraw/openDraw?id=' + id)
         
          }
      
          if (res.confirm) {
            console.log('输id',id)
            $.goto('/diaoyu/pages/diaodian/delPost/delPost?id=' + id)
            // $.post('yuxun/chouhaonow',{fangdiao_id:id,chouhaonow:1},res=>{
            //   console.log('开启成功')
            //   wx.showToast({
            //     title: '开启成功',
            //   })
             
            // })
          }
        }
      })
    },
    pinglun:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        $.goto("/d/pages/luntan/detail?id="+id);
    },
    editdiaodian:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        $.goto("/diaoyu/pages/fatie3?id="+id);
    },
    editdiaodian4:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        $.goto("/diaoyu/pages/fatie4?id="+id);
    },
    deldiaodian:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        $.post('yuxun/deldiaodian', { id: id}, function (res) {
            if(res.status==1){
                var datalist = that.data.datalist;
                datalist.splice(id,1);
                console.log(datalist)
		        that.setData({datalist:datalist});
            }else{
                $.error(res.msg);
            }
        })
    },
    // 报名情况
    goSign(e){
      console.log('909',e.currentTarget.dataset.id)
      const id = e.currentTarget.dataset.id
   
      // $.goto("/diaoyu/pages/apply?id=" + id);
      console.log('090')
    },
    // 鱼获重量
    goYuhuo(){
      // wx.navigateTo({
      //   url: '/diaoyu/pages/catchWeight',
      // })
      $.goto("/diaoyu/pages/apply");
    },
    deldiaodian4:function(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        $.post('yuxun/deldiaodian4', { id: id}, function (res) {
            if(res.status==1){
                var datalist = that.data.datalist;
                datalist.splice(id,1);
                console.log(datalist)
                that.getDataList();
            that.setData({datalist:datalist});
            that.getDataList();
            }else{
                $.error(res.msg);
            }
            that.getDataList();
        })
    }
}))